package com.service.impl;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import org.springframework.stereotype.Service;
import org.springframework.util.Base64Utils;
import org.springframework.web.multipart.MultipartFile;

import com.service.ITestService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TestServiceImpl implements ITestService {

	@Override
	public String saveFile(MultipartFile file) throws IllegalStateException, IOException {
		log.info("start to receive file==========");
		String fileName = file.getOriginalFilename();
		log.info("file info: " + fileName + "," + file.getSize() + "kb");
		File localFile = new File("D:/storage/" + fileName);
		File dir = localFile.getParentFile();
		if (!dir.isDirectory()) {
			dir.mkdirs();
		}
		file.transferTo(localFile);
		return "upload " + fileName + " success " + localFile.getCanonicalPath();

	}

	@Override
	public String getBase64Image(String path, String type) {
		byte[] bys = null;
		try {
			bys = Files.readAllBytes(new File(path).toPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "data:image/" + type + ";base64," + Base64Utils.encodeToString(bys);
	}

}
